package com.shen.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shen.commonutils.Result;
import com.shen.staservice.client.UcenterClient;
import com.shen.staservice.entity.StatisticsDaily;
import com.shen.staservice.mapper.StatisticsDailyMapper;
import com.shen.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author shenkaituo
 * @since 2020-07-10
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) {
        Result result = ucenterClient.countRegister(day);
        Integer countRegister = (Integer)result.getData().get("countRegister");
        QueryWrapper<StatisticsDaily> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        StatisticsDaily daily = baseMapper.selectOne(queryWrapper);
        if(daily!=null) baseMapper.delete(queryWrapper);


        //把数据存放到数据库

        StatisticsDaily statisticsDaily=new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);//注册人数
        statisticsDaily.setDateCalculated(day);//统计日期

        RandomUtils.nextInt(100,200);
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
//        statisticsDaily.setGmtModified(new Date());
//        statisticsDaily.setGmtCreate(new Date());
        int insert = baseMapper.insert(statisticsDaily);

    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select("date_calculated",type);//只查询两列


        List<StatisticsDaily> list = baseMapper.selectList(queryWrapper);
        // 返回日期和 对应的数量
        List<String> date=new ArrayList<>();
        List<Integer> num=new ArrayList<>();

        for( int i=0;i<list.size();i++){
            StatisticsDaily daily = list.get(i);
            date.add(daily.getDateCalculated());
            //判断是哪个--
            switch (type){
                case"login_num":
                    num.add(daily.getLoginNum());
                        break;
                case"register_num":
                    num.add(daily.getRegisterNum());
                    break;
                case"video_view_num":
                    num.add(daily.getVideoViewNum());
                    break;
                case"course_num":
                    num.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        Map<String,Object> mp=new HashMap<>();
        mp.put("date",date);
        mp.put("num",num);
        return mp;
    }
}
